package com.bzg.aop;

import com.bzg.aop.advice.BzgBeforeAdvice;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * spring提供了一个ProxyFactory来整合jdk和cglib代理
 * */
public class MyProxyFactory {

	public static void main(String[] args) {
//		ProxyFactory proxyFactory = new ProxyFactory();
//		// 添加目标类
//		proxyFactory.setTargetSource(new TargetSource() {
//			@Override
//			public Class<?> getTargetClass() {
//				return null;
//			}
//
//			@Override
//			public boolean isStatic() {
//				return false;
//			}
//
//			@Override
//			public Object getTarget() throws Exception {
//				return null;
//			}
//
//			@Override
//			public void releaseTarget(Object target) throws Exception {
//
//			}
//		});
//		//添加增强逻辑
////	    proxyFactory.addAdvice(new BzgBeforeAdvice());
//	   proxyFactory.addInterface(TestInterface.class);
//	    // 添加实现接口
////		proxyFactory.addInterface();
//		TestInterface proxy = (TestInterface)proxyFactory.getProxy();
//        proxy.equals("3");
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(TestImpl.class);
		enhancer.setCallbackFilter(new CallbackFilter() {
			@Override
			public int accept(Method method) {
				return 0;
			}
		});
		enhancer.setCallback(new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				return null;
			}
		});
		TestImpl o = (TestImpl)enhancer.create();
		o.de("");
	}
}
